<script setup lang="ts">
import { getHomeBannerAPI, getHomeCategoryAPI, getBulletinAPI } from '@/services/home'
import type { BannerItem, CategoryItem, BulletinItem } from '@/types/home'
import { onLoad, onShareAppMessage } from '@dcloudio/uni-app'
import { ref } from 'vue'
import CustomNavbar from './components/CustomNavbar.vue'
import CategoryPanel from './components/CategoryPanel.vue'

import PageSkeleton from './components/PageSkeleton.vue'
import { useGuessList } from '@/composables'

// 最近两条公告
const lastBulletin = ref<BulletinItem>()
// 获取轮播图数据
const bannerList = ref<BannerItem[]>([])
const getHomeBannerData = async () => {
  const res = await getHomeBannerAPI()
  bannerList.value = res.results
  // console.log('轮播数据：', res.results)
}

// 获取公告栏数据
const bulletinList = ref<BulletinItem[]>([])
const getHomeBulletinData = async () => {
  const res = await getBulletinAPI()
  bulletinList.value = res.results
  // console.log('所有公告：', res.results)
  // console.log('最新公告：', lastBulletin)
  lastBulletin.value = res.results[res.results.length - 1]
}

// 获取前台分类数据
const categoryList = ref<CategoryItem[]>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.results
  // console.log('分类', res.results)
}
// 是否加载中标记
const isLoading = ref(false)

// 页面加载
onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBannerData(), getHomeBulletinData(), getHomeCategoryData()])
  isLoading.value = false
})
// 右上角分享
onShareAppMessage(() => {
  return {
    title: '高中技术题库',
    path: '/pages/index/index',
  }
})

// 分享到朋友圈
const onShareTimeline = () => {
  return {
    title: '高中技术',
    path: `/pages/index/index`,
  }
}
// 猜你喜欢组合式函数调用
const { guessRef, onScrolltolower } = useGuessList()
// 当前下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开始动画
  isTriggered.value = true
  // 加载数据
  // await getHomeBannerData()
  // await getHomeCategoryData()
  // await getHomeHotData()
  // 重置猜你喜欢组件数据
  guessRef.value?.resetData()
  await Promise.all([getHomeBannerData(), getHomeBulletinData(), getHomeCategoryData()])
  // 关闭动画
  isTriggered.value = false
}
</script>

<template>
  <view class="viewport">
    <!-- 自定义导航栏 -->
    <CustomNavbar />
    <!-- 滚动容器 -->
    <scroll-view enable-back-to-top refresher-enabled @refresherrefresh="onRefresherrefresh"
      :refresher-triggered="isTriggered" @scrolltolower="onScrolltolower" class="scroll-view" scroll-y>
      <PageSkeleton v-if="isLoading" />
      <template v-else>
        <!-- 自定义轮播图 -->
        <XtxSwiper :list="bannerList" />
        <view class="bulletin">
          <text>通告：</text>
          <text>{{ lastBulletin?.title }}</text>
          <text>{{ lastBulletin?.content }}</text>
        </view>
        <!-- 分类面板 -->
        <CategoryPanel :list="categoryList" />
      </template>
    </scroll-view>
  </view>
</template>

<style lang="scss" scoped>
page {
  background-color: #f7f7f7;
  height: 100%;
  overflow: hidden;
}

.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.bulletin {
  font-size: 27rpx;
}

.scroll-view {
  flex: 1;
  overflow: hidden;
}

.bulletin {
  display: flex;
  flex-direction: column;
}
</style>
